/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package miage.sessions.persistant;

import java.rmi.RemoteException;
import javax.ejb.FinderException;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import miage.beans.ClientPresentation;
import miage.entities.Client;

/**
 *
 * @author Pepito
 */
@Stateless
public class AccountClientFacade implements AccountClientFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Client getClientByAuthentification(String login, String password)throws FinderException, RemoteException {
        System.out.println("je suis dans getClientByAuthentification");
        Client query =(Client) em.createNamedQuery("Client.findByLoginPassword").setParameter("login", login).setParameter("password", password).getSingleResult();
        System.out.println(query);
        return query;
    }
    @Override
    public Client getClientByLogin(String login){
        System.out.println("Client get client by login inside");
        return (Client) em.createNamedQuery("Client.findByLogin").setParameter("login", login).getSingleResult();
    }
    @Override
    public void addClient(ClientPresentation client)throws FinderException, RemoteException {
        System.out.println("Prêt pour insertion dans la BD");
        Client c = new Client();
        c.setAdresseClient(client.getAdresse_client());
        c.setCodepostalClient(client.getCp_client());
        c.setEmail(client.getEmail());
        c.setIbanClient(client.getIban_client());
        c.setLogin(client.getLogin());
        c.setNom(client.getNom_client());
        c.setPassword(client.getPassword());
        c.setPrenom(client.getPrenom_client());
        c.setSolde(client.getSolde());
        c.setVilleClient(client.getVille_client());
        c.setFlagClientAdmin(client.getFlag_client_admin());
        
        em.persist(c);
        System.out.println("persist ok");
    }
    @Override
    public void deleteClient(String login, String password)throws FinderException, RemoteException {
        Client client =(Client) em.createNamedQuery("Client.findByLoginPassword").setParameter("login", login).setParameter("password", password).getSingleResult();
        em.remove(client);
    }
    @Override
    public void updateClient(ClientPresentation client)throws FinderException, RemoteException {
        try{
            System.out.println("client dans Up "+client.toString());
            Client c = (Client)em.find(Client.class, client.getIban_client());
            System.out.println(client.getIban_client());
            c.setAdresseClient(client.getAdresse_client());
            c.setCodepostalClient(client.getCp_client());
            c.setEmail(client.getEmail());
            c.setLogin(client.getLogin());
            c.setNom(client.getNom_client());
            c.setPassword(client.getPassword());
            c.setPrenom(client.getPrenom_client());
            c.setSolde(client.getSolde());
            c.setVilleClient(client.getVille_client());
            c.setFlagClientAdmin(client.getFlag_client_admin());
            
            em.persist(c);
          
            System.out.println("update client ok");
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    @Override
    public Client getClientByIban(String ibanClient) throws FinderException, RemoteException {
        return (Client) em.createNamedQuery("Client.findByIbanClient").setParameter("ibanClient", ibanClient).getSingleResult();
    }
}
